<?php

class Application_Model_GrandprixDAO
{
	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if(is_string($dbTable))
		{
			$dbTable = new $dbTable();
		}
		if(!$dbTable instanceof Zend_Db_Table_Abstract)
		{
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
 
	public function getDbTable()
	{
		if(null === $this->_dbTable)
		{
			$this->setDbTable('Application_Model_DbTable_Grandprix');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_Grandprix $grandprix)
	{
		$data = array(
			'GPNUM' => $grandprix->getGpNum(),
			'CIRNUM' => $grandprix->getCircuit()->getCirNum(),
			'GPNOM' => $grandprix->getGpNom(),
			'GPDATE' => $grandprix->getGpDate(),
			'GPNBTOURS' => $grandprix->getGpNbTours(),
			'GPDATEMAJ' => $grandprix->getGpDateMaj(),
			'GPCOMMENTAIRE' => $grandprix->getGpCommentaire()
		);
 
		if (null === ($gpNum = $grandprix->getGpNum()))
		{
			echo 'nouveau';
			unset($data['GPNUM']);
			$this->getDbTable()->insert($data);
        }
        else
        {
        	echo 'update';
			$this->getDbTable()->update($data, array('GPNUM = ?' => $gpNum));
		}
		return true;
	}
	
	public function delete($id)
	{
		$courseDbTable = new Application_Model_DbTable_Course();
		$courseDbTable->delete(array('GPNUM = ?' => $id));
		$essaisDbTable = new Application_Model_DbTable_Essais();
		$essaisDbTable->delete(array('GPNUM = ?' => $id));
		$this->getDbTable()->delete(array('GPNUM = ?' => $id));
	}
	
	public function find($gpNum)
	{
		$grandprix = new Application_Model_Grandprix();
		$piloteDAO = new Application_Model_PiloteDAO();
		$result = $this->getDbTable()->find($gpNum);
		if(0 == count($result))
		{
			return null;
		}
		$row = $result->current();
		$grandprix->setGpNum($row->GPNUM)
				  ->setGpNom($row->GPNOM)
				  ->setGpDate($row->GPDATE)
				  ->setGpNbTours($row->GPNBTOURS)
				  ->setGpDateMaj($row->GPDATEMAJ)
				  ->setGpCommentaire($row->GPCOMMENTAIRE);
				  
		//On recupere le circuit
		$circuitDAO = new Application_Model_CircuitDAO();
		$circuit = $circuitDAO->find($row->CIRNUM);
		$grandprix->setCircuit($circuit);
		
		//On recupere la course
		$courseDAO = new Application_Model_CourseDAO();
		$course = $courseDAO->findByGrandprix($gpNum);
		$grandprix->setCourse($course);
		
		return $grandprix;
	}
	
	public function findAll()
	{
		$resultSet = $this->getDbTable()->fetchAll($this->getDbTable()->select()->from('grandprix',array('GPNUM'))->order('GPNOM ASC'));
		$entries = array();
		foreach($resultSet as $row)
		{
			$entries[] = $this->find((int)$row->GPNUM);
		}
		return $entries;
	}
	
	public function findTop5()
	{
		$resultSet = $this->getDbTable()->fetchAll($this->getDbTable()->select()->from('grandprix',array('GPNUM'))->order('GPDATEMAJ DESC')->limit(5,0));
		$entries = array();
		foreach($resultSet as $row)
		{
			$entries[] = $this->find((int)$row->GPNUM);
		}
		return $entries;
		/*
		$db = Zend_Db_Table::getDefaultAdapter();
		$resultSet = $db->query('select gp.GPNUM, gp.CIRNUM, gp.GPNOM, gp.GPDATE, gp.GPNBTOURS, gp.GPDATEMAJ, gp.GPCOMMENTAIRE, TEMPSCOURSE, PILNOM, PILPRENOM, PHOADRESSE from grandprix gp 
								inner join course c on gp.GPNUM = c.GPNUM
								inner join pilote p on c.PILNUM = p.PILNUM
								inner join photo ph on p.PILNUM = ph.PILNUM
								group by gp.GPNUM, gp.CIRNUM, gp.GPNOM, gp.GPDATE, gp.GPNBTOURS, gp.GPDATEMAJ, gp.GPCOMMENTAIRE, TEMPSCOURSE, PILNOM, PILPRENOM, PHOADRESSE
								having TEMPSCOURSE <= all(select TEMPSCOURSE from course where gp.GPNUM = GPNUM)
								order by gp.GPDATEMAJ DESC
								LIMIT 0, 5')->fetchAll(zend_db::FETCH_OBJ);
		
		return $resultSet;*/
	}
	
	public function findLast()
	{
		$grandprix = new Application_Model_Grandprix();
		$row = $this->getDbTable()->fetchRow($this->getDbTable()->select()->from('grandprix',array('GPNUM'))->where('GPDATEMAJ >= all(SELECT GPDATEMAJ FROM grandprix)'));
		$grandprix = $this->find($row->GPNUM);
		return $grandprix;
	}
}